#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "airmouse.h"



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    createActions();
    createTrayIcon();
    connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
    trayIcon->show();
    Qt::WindowFlags flags = Qt::Window | Qt::WindowCloseButtonHint;
    this->setWindowFlags(flags);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    //start
    controller.addListener(airmouse);
    controller.setPolicyFlags(Controller::POLICY_BACKGROUND_FRAMES);
}

void MainWindow::on_pushButton_2_clicked()
{
    //stop
    controller.removeListener(airmouse);
}

void MainWindow::createTrayIcon(){
    trayIconMenu = new QMenu(this);
    //trayIconMenu->addAction(minimizeAction);
    trayIconMenu->addAction(AirMouseAction);
    //checkbox neben airmouseaction!
    trayIconMenu->addAction(restoreAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(quitAction);

    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setContextMenu(trayIconMenu);
    QIcon icon = QIcon(":/images/airmouse.png"), tr("AirMouse");
    trayIcon->setIcon(icon);
    setWindowIcon(icon);
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    if (trayIcon->isVisible()) {
        //QMessageBox::information(this, tr("Systray"),tr("The program will keep running in the system tray. To terminate the program, choose <b>Quit</b> in the context menu of the system tray entry."));
        hide();
        event->ignore();
    }
}

void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason){
    switch (reason) {
    case QSystemTrayIcon::Trigger:
    case QSystemTrayIcon::DoubleClick:
        showNormal();

        break;
    case QSystemTrayIcon::MiddleClick:
        showNormal();
        break;
    default:
        ;
    }
}

void MainWindow::createActions()
{
    //minimizeAction = new QAction(tr("Mi&nimize"), this);
    //connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
    AirMouseAction = new QAction(tr("AirMouse"), this);
    connect(AirMouseAction, SIGNAL(triggered()), this, SLOT(AirMouseSS()));

    restoreAction = new QAction(tr("&Restore"), this);
    connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));

    quitAction = new QAction(tr("&Quit"), this);
    connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
}

void MainWindow::on_spinBox_valueChanged(int arg1)
{
    airmouse.setModus(arg1);
}

void MainWindow::on_horizontalSlider_valueChanged(int value)
{
    airmouse.setnormalvariable(value);
}

void MainWindow::on_horizontalSlider_2_valueChanged(int value)
{
    airmouse.setvelovariable(value);
}

void MainWindow::on_horizontalSlider_actionTriggered(int action)
{

}

void MainWindow::on_horizontalSlider_2_actionTriggered(int action)
{

}

void MainWindow::on_horizontalSlider_3_valueChanged(int value)
{
    airmouse.setbasefactor(value);
}
